<?php
namespace common\models;




class Article extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'article';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['class_id', 'create_time', 'update_time'], 'integer'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 32],
            [['desc'], 'string', 'max' => 128],
            [['logo'], 'string', 'max' => 200],
            [['state'], 'string', 'max' => 1],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'class_id' => '公告分类ID',
            'title' => '标题',
            'desc' => '简介',
            'logo' => 'logo图片',
            'content' => '内容',
            'state' => '1显示 0不显示',
            'create_time' => '创建时间',
            'update_time' => 'Update Time',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
        $model  = self::find();
        $where  = $whereArr['where'];
        $whereAnd = isset($whereArr['whereAnd'])?$whereArr['whereAnd']:[];
        $models = self::queryFormart($model,$where,$params,$whereAnd);
        $model  = $models['model'];
        self::$pages = $models['pages'];

        $data  = $model->asarray()->all();
        if(!$data) return array();

        //扩展信息
        if(!$extends) return $data;
        foreach($data as &$val)
        {
            foreach($extends as $eType)
            {
                if($eType=='class')
                {
                    $class = ArticleClass::getOne(['id'=>$val['class_id']]);
                    $val['class_name'] = $class['class_name'];
                }
            }
        }
        return $data;
    }

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

}